<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="phoneData" type="text" maxlength="11" placeholder="用户名/电话" :focus="isFocus"></wInput>
				<wInput v-model="passData" type="password" maxlength="11" placeholder="密码"></wInput>
			</view>
			<wButton class="wbutton" text="登 录" :rotate="isRotate" @click="startLogin"></wButton>

			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view>

			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import store from '../../store/index';
	import Vue from 'vue';

	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '../../static/img/logo.png',
				phoneData: '', //用户/电话
				passData: '', //密码
				isRotate: false, //是否加载旋转
				isFocus: true, // 是否聚焦
				userData: [],
			};
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {
			_this = this;
			this.isLogin();
		},
		methods: {
			isLogin() {
				//判断缓存中是否登录过，直接登录
				try {
					if (this.$store.getters.isLoggedIn) {
						//有登录信息
						uni.navigateTo({
							url: '/pages/map/mapStu', // 使用绝对路径
						});
					}
				} catch (e) {
					// error
				}
			},
			startLogin(e) {
				console.log(e)
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '用户名不能为空'
					});
					return;
				}
				if (this.passData.length < 5) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不正确'
					});
					return;
				}

				// console.log("登录成功")

				_this.isRotate = true
				setTimeout(function() {
					_this.isRotate = false
				}, 3000)
				uni.showLoading({
					title: '登录中'
				});

				uni.request({
					url: 'http://localhost:8080/app/user/login?userName=' +
						this.phoneData + '&password=' + this.passData,
					method: 'GET',
					success: res => {
						if (res.statusCode === 200 && res.data.code === 0) {
							this.userData = res.data.result;
							console.log(this.userData);
							if (this.userData[0] != 'succeed') {
								_this.passData = ""
								uni.showToast({
									icon: 'error',
									position: 'bottom',
									title: '账号或密码错误'
								});
								uni.hideLoading();
							} else {
								//保存登录数据
								this.$store.commit('login', this.userData);
								this.changeMap();

								console.log('有点成功');
							}
						} else {
							uni.showToast({
								icon: 'error',
								position: 'bottom',
								title: '服务器请求失败'
							});
							uni.hideLoading();
						}
					},
					fail: err => {
						console.log('请求失败', err);
						uni.hideLoading();
					}
				});

			},
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			changeMap() {
				// 在登录成功后获取用户信息，假设 userRole 是从服务器获取的用户角色信息
				let userTye = this.userData[2]; // 假设当前用户是学生

				// 根据用户角色动态修改 tabBar 的内容
				if (userTye === '0') {
					// uni.switchTab({
					// 	url: '/pages/map/mapStu', // 使用绝对路径
					// });
					uni.redirectTo({
						url: '/pages/map/mapStu'
					});
				} else {
					uni.redirectTo({
						url: '/pages/map/map'
					});
				}
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>